/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import kr.pe.johnkim.dayve.activity.R;
import kr.pe.johnkim.dayve.util.DayveCommon;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author johnkim
 *
 */
public class DayveItemAdapter extends BaseAdapter {
	public static final String TAG = DayveItemAdapter.class.getSimpleName();

	private Context context;
	private ArrayList<DayveItem> data;
	private DayveItem item;
	LayoutInflater inf;
	
	View view;
	
	DayveCommon dayveCommon = new DayveCommon();
	
	public DayveItemAdapter(Context context, ArrayList<DayveItem> data) {
		this.context = context;
		this.data = data;
		inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		view = convertView;
		
		ViewWrapper wrapper = null;
		
		if(view == null) {
			view = inf.inflate(R.layout.dayve_item_row, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		item = data.get(position);
		
		// Item Name 을 세팅 한다.
		wrapper.getTxtViewItemName().setText("  " + item.getName() + "  ");
		
		// Register Date 를 세팅 한다.
		wrapper.getTxtViewItemRegisterDate().setText("  " + dayveCommon.getOnlyDateDateFormat(item.getModifyDate()) + "  ");

		// Item Amount 를 세팅 한다.
		wrapper.getTxtViewItemAmount().setText(item.getAmount());
		
		wrapper.getTxtViewItemRegisterDateTime().setText(dayveCommon.getOnlyTimeDateFormat(item.getModifyDate()));
		
		return view;
	}

	
	/**
	 * View Wrapper class.
	 * 
	 * @author johnkim
	 *
	 */
	class ViewWrapper {
		View base;
		
		TextView txtViewItemName;					// Item Name.
		TextView txtViewItemRegisterDate;			// Register Date.
		TextView txtViewItemAmount;					// Item Amount.
		TextView txtViewItemRegisterDateTime;		// Register Date Time.
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		TextView getTxtViewItemName() {
			if(txtViewItemName == null) {
				txtViewItemName = (TextView)view.findViewById(R.id.txtViewItemName);
			}
			
			return (txtViewItemName);
		}
		
		TextView getTxtViewItemRegisterDate() {
			if(txtViewItemRegisterDate == null) {
				txtViewItemRegisterDate = (TextView)view.findViewById(R.id.txtViewItemRegisterDate);
			}
			
			return (txtViewItemRegisterDate);
		}
		
		TextView getTxtViewItemAmount() {
			if(txtViewItemAmount == null) {
				txtViewItemAmount = (TextView)view.findViewById(R.id.txtViewItemAmount);
			}
			
			return (txtViewItemAmount);
		}
		
		TextView getTxtViewItemRegisterDateTime() {
			if(txtViewItemRegisterDateTime == null) {
				txtViewItemRegisterDateTime = (TextView)view.findViewById(R.id.txtViewItemRegisterDateTime);
			}
			
			return (txtViewItemRegisterDateTime);
		}
	}
}
